package com.ykq.system.service.impl;

import com.ykq.pojo.dto.CommonResult;
import com.ykq.pojo.entity.Permission;
import com.ykq.system.dao.PermissionMapper;
import com.ykq.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: education-parent
 * @description:
 * @author: 闫克起2
 * @create: 2022-08-03 11:34
 **/
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public List<String> selectByUserId(String id) {
        List<String> list=permissionMapper.selectByUserId(id);
        return list;
    }

    @Override
    public CommonResult selectMenuByUserId() {

        //查询当前用户具有的菜单
        List<Permission> list=permissionMapper.selectMenuByUserId("1");
        //设置菜单的层级关系。
        List<Permission> firstrp=new ArrayList<Permission>();
        for (Permission p:list){
            if (p.getPid().equals("1")){
                firstrp.add(p);
            }
        }
        for (Permission f:firstrp){
            f.setChildren(getChildren(list,f.getId()));
        }
        return new CommonResult(2000,"查询左侧菜单成功",firstrp);
    }


    private List<Permission> getChildren(List<Permission> rp,String pid){
        List<Permission> children=new ArrayList<Permission>();
        for (Permission m:rp){
            if (m.getPid().equals(pid)){
                children.add(m);
            }
        }
        for (Permission c:children){
            c.setChildren(getChildren(rp,c.getId()));
        }
        return children;
    }
}















